package com.jams.goodox.activity.mine.message;

import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.net.BaseEntity;
import com.jams.goodox.net.ProtocolManagers;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.Utils;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/10.
 */

public class MineMessageActivity extends BaseActivity {
    @Bind(R.id.recycle_view)
    RecyclerView recycleView;
    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    private List<MineMessageBean> mineMessageBeanList;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_message_layout;
    }

    @Override
    protected void init() {
        StyleUtil.init(this, "我的消息");
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部

        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        mineMessageBeanList=new ArrayList<>();
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(new RecycleCommonAdapter<MineMessageBean>(this, mineMessageBeanList, R.layout.mine_message_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, MineMessageBean item, final int position) {
                helper.setText(R.id.tx_message_type,"课程消息");
                helper.setText(R.id.tx_info,item.getContent());
                helper.setText(R.id.tx_time, Utils.formatDateTime(item.getCreateTime()));
            }
        });
        getData();
        swipeRefresh.setRefreshing(true);
    }

    private void getData(){
        enqueue(createCallService(MineService.class).queueMessage(ProtocolManagers.getDefaultMap()), new RetrofitCallbackListener<BaseEntity>() {
            @Override
            public void onSuccess(Call<BaseEntity> call, BaseEntity result) {
                if (TextUtils.isEmpty(result.getResponseData())) return;
                swipeRefresh.setRefreshing(false);
                try{
                    JsonArray jsonArray = new JsonParser().parse(result.getResponseData()).getAsJsonArray();
                    mineMessageBeanList.clear();
                    for(JsonElement jsonElement:jsonArray){
                        MineMessageBean message = new Gson().fromJson(jsonElement, MineMessageBean.class);
                        mineMessageBeanList.add(message);
                    }
                    recycleView.getAdapter().notifyDataSetChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String msg) {
                swipeRefresh.setRefreshing(false);
            }
        });
    }

}
